package sybel.pan;

import java.awt.Component;
import javax.swing.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import sybel.DOMPanelInterface;

/**
 *
 * @author Stefano
 */
public class ActionExecute extends javax.swing.JPanel implements DOMPanelInterface{

    /** Creates new form ActionExecute */
    public ActionExecute(Node ActExeFather) {
        Parent = ActExeFather;
        NodeList nl = ((Element)ActExeFather).getElementsByTagName("actionExecute");
        actionExecute = (Element)nl.item(0);
        initComponents();
        if(actionExecute != null)
            initPanel();
        annidation++;
    }

    public ActionExecute(Node ActExeFather,int count) {
        Parent = ActExeFather;
        NodeList nl = ((Element)ActExeFather).getElementsByTagName("actionExecute");
        actionExecute = (Element)nl.item(0);
        initComponents();
        if(actionExecute != null)
           initPanel();
        annidation = count + 1;
        if(annidation == 3){
            jCBEntity.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Choose one...", "entity","variable" }));
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jCBEntity = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTextAction = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        jTextParam1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jCBValue1 = new javax.swing.JComboBox();
        jTextValue1 = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jTextParam2 = new javax.swing.JTextField();
        jCBValue2 = new javax.swing.JComboBox();
        jTextValue2 = new javax.swing.JTextField();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Action Execute"));

        jLabel1.setText("Entity:");

        jCBEntity.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Choose one...", "entity", "variable", "action Execute", "property Get" }));
        jCBEntity.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCBEntityItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 15, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 20, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGap(6, 6, 6)
                .addComponent(jCBEntity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(jCBEntity))
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        jPanel3Layout.setHonorsVisibility(false);
        jPanel3.setVisible(false);

        jLabel2.setText("Action:");

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Arguments"));

        jLabel3.setText("Parameter");

        jLabel4.setText("Value");

        jCBValue1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "baseValue", "entity", "variable" }));

        jCBValue2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "baseValue", "entity", "variable" }));

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextParam1, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel4)
                            .addComponent(jTextValue1, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)
                            .addComponent(jCBValue1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jTextParam2, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jCBValue2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jTextValue2, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextParam1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCBValue1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextValue1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextParam2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCBValue2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextValue2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextAction, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextAction, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jCBEntityItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCBEntityItemStateChanged
       EntitySelector();
    }//GEN-LAST:event_jCBEntityItemStateChanged

    /**
     * Metodo per inizializzare il pannello Action Execute
     */
    public void initPanel() {
        Node actExeChild = actionExecute.getFirstChild();
        JPanel entity = null;
        // verifico di che tipo è l'entità a cui si riferisce la action
        if(actExeChild.getNodeName().contains("entity")){
            jCBEntity.setSelectedIndex(1);
            entity = new Entity(actionExecute);
        }
        if(actExeChild.getNodeName().contains("variable")){
            jCBEntity.setSelectedIndex(2);
            entity = new Variable(actionExecute);
        }
        if(actExeChild.getNodeName().contains("actionExecute")){
            jCBEntity.setSelectedIndex(3);
            entity = new ActionExecute(actionExecute, annidation);
        }
        if(actExeChild.getNodeName().contains("propertyGet")){
            jCBEntity.setSelectedIndex(4);
            entity= new PropertyGet(actionExecute,annidation);
        }
        setEntityPanel(entity);

        // settaggio dell'action e suoi parametri
        String action = actionExecute.getAttribute("action");
        if(action != null){
            jTextAction.setText(action);
            Element argument = (Element)actExeChild.getNextSibling();
            int count = 0; // utile a capire se mi sto riferendo al primo o al secondo parametro
            while(argument != null){
                if(argument.getNodeName() == "argument"){
                    switch(count){
                        case 0:
                            jTextParam1.setText(argument.getAttribute("parameter"));
                            jTextValue1.setText(action);
                            String typeName = argument.getFirstChild().getNodeName();
                            if(typeName == "baseValue")
                                jCBValue1.setSelectedIndex(0);
                            if(typeName == "entity")
                                jCBValue1.setSelectedIndex(1);
                            if(typeName == "variable")
                                jCBValue1.setSelectedIndex(2);
                            break;

                        case 1:
                            jTextParam2.setText(argument.getAttribute("parameter"));
                            jTextValue2.setText(action);
                            String typename = argument.getFirstChild().getNodeName();
                            if(typename == "baseValue")
                                jCBValue2.setSelectedIndex(0);
                            if(typename == "entity")
                                jCBValue2.setSelectedIndex(1);
                            if(typename == "variable")
                                jCBValue2.setSelectedIndex(2);
                            break;
                    }
                    count++;
                }
                argument = (Element)argument.getNextSibling();
            }
        }
    }

    private void EntitySelector(){
        switch(jCBEntity.getSelectedIndex()){
           // Entity
            case 1:
                Entity entity;
                if(actionExecute == null){
                    entity = new Entity(Parent);
                }else{
                    entity = new Entity(actionExecute);
                }
                setEntityPanel(entity);
                break;

             // Variable
            case 2:
                Variable variable;
                if(actionExecute == null){
                    variable = new Variable(Parent);
                }else{
                    variable = new Variable(actionExecute);
                }
                setEntityPanel(variable);
                break;

            // Action Execute
            case 3:
                ActionExecute AEPanel;
                if(actionExecute == null){
                    AEPanel = new ActionExecute(Parent,annidation);
                }else{
                    AEPanel = new ActionExecute(actionExecute,annidation);
                }
                setEntityPanel(AEPanel);
                break;

            // Property Get
            case 4:
                PropertyGet propertyGet;
                if(actionExecute == null){
                    propertyGet = new PropertyGet(Parent,annidation);
                }else{
                    propertyGet = new PropertyGet(actionExecute,annidation);
                }
                setEntityPanel(propertyGet);
                break;
        }
    }

    private void setEntityPanel(JPanel entity){
        /* Imposto il layout inserendo il pannello Action Execute nel pannello
         * adibito ad ospitare i componenti scelti nella combobox   */
        jPanel3.removeAll();
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(entity, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(entity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                );

        // rendo visibile il nuovo pannello
        jPanel3.repaint();
        jPanel3.setVisible(true);
    }

    /** 
     * Metodo per il salvataggio dei dati contenuti nelpannello
     * @return il nodo actionExecute
     */
    public Node saveData(){
        Document doc = Parent.getOwnerDocument();
        actionExecute = doc.createElement("actionExecute");
        Component[] comp = jPanel3.getComponents();
        for(int i= 0; i < comp.length; i++){
            if(comp[i] instanceof DOMPanelInterface)
                actionExecute.appendChild(((DOMPanelInterface)comp[i]).saveData());
        }

        // questa parte dei parametri andrebbe rivista
        if(!jTextAction.getText().isEmpty()){
            actionExecute.setAttribute("action", jTextAction.getText());

            if(!jTextParam1.getText().isEmpty()){
                if(!jTextValue1.getText().isEmpty()){
                    Element argument1 = doc.createElement("argument");
                    argument1.setAttribute("parameter",jTextParam1.getText());
                    Element argValue1 = doc.createElement(jCBValue1.getSelectedItem().toString());
                    argValue1.setTextContent(jTextValue1.getText());
                    argument1.appendChild(argValue1);
                    actionExecute.appendChild(argument1);
                }
            }

            if(!jTextParam2.getText().isEmpty()){
                if(!jTextValue2.getText().isEmpty()){
                    Element argument2 = doc.createElement("argument");
                    argument2.setAttribute("parameter",jTextParam2.getText());
                    Element argValue2 = doc.createElement(jCBValue2.getSelectedItem().toString());
                    argValue2.setTextContent(jTextValue2.getText());
                    argument2.appendChild(argValue2);
                    actionExecute.appendChild(argument2);
                }
            }

        }
        return actionExecute;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jCBEntity;
    private javax.swing.JComboBox jCBValue1;
    private javax.swing.JComboBox jCBValue2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTextAction;
    private javax.swing.JTextField jTextParam1;
    private javax.swing.JTextField jTextParam2;
    private javax.swing.JTextField jTextValue1;
    private javax.swing.JTextField jTextValue2;
    // End of variables declaration//GEN-END:variables
    private int annidation = 0;
    private Element actionExecute;
    private Node Parent;

}
